@namespace AntSK.Pages.Setting.ChatHistory
@page "/setting/chathistory"
@using AntSK.Services.Auth
@inherits AuthComponentBase
@using Microsoft.AspNetCore.Authorization
@using System.ComponentModel
@using AntDesign.TableModels
@using AntSK.Domain.Repositories
@using AntSK.Domain.Common.Map
@attribute [Authorize(Roles = "AntSKAdmin")]

<div class="chat-history-container">
    <Card>
        <TitleTemplate>
            <div class="page-header">
                <div class="header-left">
                    <Icon Type="message" Theme="outline" />
                    <Title Level="2" style="margin-bottom: 0; margin-left: 8px;">聊天记录</Title>
                </div>
                <div class="header-right">
                    <Search 
                        Placeholder="搜索用户名或消息内容" 
                        @bind-Value="searchString" 
                        OnSearch="Search"
                        EnterButton="true"
                        Size="@InputSize.Large"
                        style="width: 300px;" />
                </div>
            </div>
        </TitleTemplate>
        <Body>
            <Table @ref="table"
                   TItem="ChatsDto"
                   DataSource="@chatDtoList"
                   Total="_total"
                   @bind-PageIndex="_pageIndex"
                   @bind-PageSize="_pageSize"
                   OnChange="OnChange"
                   Size="TableSize.Middle"
                   RowKey="x=>x.Id"
                   Bordered="true"
                   Loading="_loading">
                <ColumnDefinitions>
                    <PropertyColumn Property="c=>c.Id" Width="80px">
                        <TitleTemplate>
                            <strong>ID</strong>
                        </TitleTemplate>
                    </PropertyColumn>
                    
                    <PropertyColumn Title="用户" Property="c=>c.UserName" Width="120px">
                        <TitleTemplate>
                            <strong><Icon Type="user" /> 用户</strong>
                        </TitleTemplate>
                    </PropertyColumn>
                    
                    <PropertyColumn Title="应用" Property="c=>c.AppName" Width="150px">
                        <TitleTemplate>
                            <strong><Icon Type="appstore" /> 应用</strong>
                        </TitleTemplate>
                    </PropertyColumn>
                    
                    <PropertyColumn Title="类型" Property="c=>c.SendReveice" Width="100px">
                        <TitleTemplate>
                            <strong><Icon Type="swap" /> 类型</strong>
                        </TitleTemplate>
                    </PropertyColumn>
                    
                    <PropertyColumn Title="消息内容" Property="c=>c.Context" Width="40%">
                        <TitleTemplate>
                            <strong><Icon Type="message" /> 消息内容</strong>
                        </TitleTemplate>
                    </PropertyColumn>
                    
                    <PropertyColumn Title="时间" Property="c=>c.CreateTime" Format="yyyy-MM-dd HH:mm:ss" Width="180px">
                        <TitleTemplate>
                            <strong><Icon Type="clock-circle" /> 时间</strong>
                        </TitleTemplate>
                    </PropertyColumn>
                </ColumnDefinitions>
            </Table>
        </Body>
    </Card>
</div>

<style>
    .chat-history-container {
        padding: 24px;
        min-height: 100vh;
    }

    .ant-card {
        border-radius: 8px;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
        border: 1px solid rgba(0, 0, 0, 0.06);
    }

    .page-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 20px;
        padding: 20px 0;
        border-bottom: 1px solid #f0f0f0;
    }

    .header-left {
        display: flex;
        align-items: center;
    }

    .header-left .anticon {
        font-size: 24px;
        color: #1890ff;
        margin-right: 12px;
    }

    .header-left .ant-typography {
        color: rgba(0, 0, 0, 0.85);
        font-weight: 600;
        margin: 0;
    }

    .header-right {
        display: flex;
        align-items: center;
    }

    .ant-input-search-large {
        border-radius: 6px;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
        transition: all 0.3s ease;
    }

    .ant-table {
        border-radius: 6px;
        overflow: hidden;
    }

    .ant-table-thead > tr > th {
        background-color: #fafafa;
        font-weight: 600;
        border-bottom: 1px solid #f0f0f0;
    }

    .ant-table-tbody > tr {
        transition: all 0.3s ease;
    }

    .ant-table-tbody > tr:hover > td {
        background-color: #f5f5f5;
    }

    .message-content {
        max-width: 400px;
    }

    .message-content .ant-typography {
        background: #fafafa;
        padding: 12px;
        border-radius: 6px;
        border-left: 4px solid #1890ff;
        margin: 0;
        line-height: 1.6;
    }

    .time-cell {
        text-align: center;
        background: #fafafa;
        padding: 8px;
        border-radius: 6px;
        border: 1px solid #e8e8e8;
    }

    .time-cell .date {
        font-size: 13px;
        color: #595959;
        font-weight: 600;
        margin-bottom: 4px;
    }

    .time-cell .time {
        font-size: 11px;
        color: #8c8c8c;
        font-family: 'Courier New', monospace;
    }

    .ant-tag {
        margin: 0;
        border-radius: 4px;
        font-weight: 500;
        padding: 4px 8px;
        border: 1px solid rgba(0, 0, 0, 0.06);
        transition: all 0.3s ease;
    }

    /* 响应式设计 */
    @@media (max-width: 1200px) {
        .message-content {
            max-width: 300px;
        }
    }

    @@media (max-width: 768px) {
        .chat-history-container {
            padding: 12px;
        }
        
        .page-header {
            flex-direction: column;
            gap: 16px;
            padding: 16px 0;
        }
        
        .header-left .ant-typography {
            font-size: 20px;
        }
        
        .header-right {
            width: 100%;
        }
        
        .header-right .ant-input-search {
            width: 100% !important;
        }

        .message-content {
            max-width: 200px;
        }
    }

    @@media (max-width: 480px) {
        .message-content {
            max-width: 150px;
        }
        
        .time-cell {
            padding: 4px;
        }
        
        .time-cell .date {
            font-size: 11px;
        }
        
        .time-cell .time {
            font-size: 10px;
        }
    }
</style>

@using System.Text.Json;
@code {
    [Inject] IChats_Repositories _chats_Repositories { get; set; }

    ChatsDto[] chatDtoList = Array.Empty<ChatsDto>();
    ITable table;
    bool _loading = false;

    int _pageIndex = 1;
    int _pageSize = 10;
    int _total = 0;
    string searchString = string.Empty;

    protected override async Task OnInitializedAsync()
    {
        await InitData();
    }

    public async Task OnChange(QueryModel<ChatsDto> queryModel)
    {
        _loading = true;
        StateHasChanged();
        await InitData();
        _loading = false;
        StateHasChanged();
    }

    private async Task InitData()
    {
        try
        {
            if (string.IsNullOrEmpty(searchString))
            {
                _total = _chats_Repositories.Count(p => true);
                var chatList = _chats_Repositories.GetDB().Queryable<Chats, Apps>((c, a) => new object[] {
                    SqlSugar.JoinType.Left, c.AppId == a.Id
                }).Select((c, a) => new ChatsDto
                {
                    Id = c.Id,
                    UserName = c.UserName,
                    AppId = c.AppId,
                    IsSend = c.IsSend,
                    SendReveice = c.IsSend ? "发送" : "接收",
                    Context = c.Context,
                    CreateTime = c.CreateTime,
                    AppName = a.Name ?? "未知应用"
                }).OrderByDescending(c => c.CreateTime).ToPageList(_pageIndex, _pageSize);
                
                chatDtoList = chatList.ToArray();
            }
            else
            {
                _total = _chats_Repositories.Count(p => p.UserName.Contains(searchString) || p.Context.Contains(searchString));
                var chatList = _chats_Repositories.GetDB().Queryable<Chats, Apps>((c, a) => new object[] {
                    SqlSugar.JoinType.Left, c.AppId == a.Id
                }).Select((c, a) => new ChatsDto
                {
                    Id = c.Id,
                    UserName = c.UserName,
                    AppId = c.AppId,
                    IsSend = c.IsSend,
                    SendReveice = c.IsSend ? "发送" : "接收",
                    Context = c.Context,
                    CreateTime = c.CreateTime,
                    AppName = a.Name ?? "未知应用"
                }).Where(c => c.UserName.Contains(searchString) || c.Context.Contains(searchString))
                .OrderByDescending(c => c.CreateTime).ToPageList(_pageIndex, _pageSize);
                
                chatDtoList = chatList.ToArray();
            }
        }
        catch
        {
            // 处理异常，可以添加日志记录
            chatDtoList = Array.Empty<ChatsDto>();
        }
    }

    private async Task Search(string searchKey)
    {
        _pageIndex = 1; // 重置到第一页
        _loading = true;
        StateHasChanged();
        await InitData();
        _loading = false;
        StateHasChanged();
    }

    public class ChatsDto : Chats
    {
        public string AppName { get; set; } = string.Empty;
        public string SendReveice { get; set; } = string.Empty;
    }
} 